package se.study.socket;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;

/**
 * @Author: tiger
 * @Date:2018/6/29
 * @Description: 复习socket网络编程
 */
public class SocketDemo01 {
    public static void main(String[] args) throws Exception {
        urlTest();

    }

    /**
     * 读取url上的资源
     */
    static void urlReadTest() throws IOException {
        URL url = new URL("http://www.baidu.com");
        // 获取url字节流
        InputStream inputStream = url.openStream();
        // 转为字符流，指定编码
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        // 添加到缓冲流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        // 读取
        String s = bufferedReader.readLine();
        while (s != null) {
            System.out.println(s);
            s = bufferedReader.readLine();
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
    }

    /**
     * URL(Uniform Resource Locator)统一资源定位符，表示Internet上某一资源的地址，协议名：资源名称
     */
    static void urlTest() throws MalformedURLException {
        URL baidu = new URL("https://www.baidu.com");
        System.out.println("协议:" + baidu.getProtocol());
        System.out.println("主机:" + baidu.getHost());
        System.out.println("文件路径:" + baidu.getPath());
        System.out.println("相对路径:" + baidu.getRef());
        System.out.println("端口:" + baidu.getPort());
        System.out.println("默认端口:" + baidu.getDefaultPort());
        System.out.println("文件名:" + baidu.getFile());
        System.out.println("参数:" + baidu.getQuery());
        System.out.println("------------------------------------------");
        URL url2 = new URL(baidu, "/index.html?username=tom#test");
        System.out.println("协议:" + url2.getProtocol());
        System.out.println("主机:" + url2.getHost());
        System.out.println("文件路径:" + url2.getPath());
        System.out.println("相对路径:" + url2.getRef());
        System.out.println("端口:" + url2.getPort());
        System.out.println("默认端口:" + url2.getDefaultPort());
        System.out.println("文件名:" + url2.getFile());
        System.out.println("参数:" + url2.getQuery());
    }

    /**
     * InetAddress类用于标识网络上的硬件资源，标识互联网协议(IP)地址。
     *
     * @throws UnknownHostException
     */
    static void addressTest() throws UnknownHostException {
        InetAddress inetAddress = Inet4Address.getLocalHost();
        System.out.println("地址:" + JSON.toJSONString(inetAddress.getAddress()));
        System.out.println("IP地址:" + inetAddress.getHostAddress());
        System.out.println("计算机名:" + inetAddress.getHostName());
        System.out.println("------------------------------------------");
        InetAddress address = InetAddress.getByName("www.baidu.com");
        System.out.println("IP地址:" + address.getHostAddress());
        System.out.println("计算机名:" + address.getHostName());
        System.out.println("------------------------------------------");
        InetAddress address2 = InetAddress.getByName("180.97.33.107");
        System.out.println("IP地址:" + address2.getHostAddress());
        System.out.println("计算机名:" + address2.getHostName());
    }
}

